package com.netstar.util;

import java.lang.reflect.Method;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiManager;

/**
 * 
 * @ClassName NetUtil
 * @Description 网络工具类
 * @author zou.sq
 * @date 2013-4-12
 */
public class NetUtil {

	private static final String TAG = "NetUtil";

	/**
	 * @Title: CheckNetworkState
	 * @Description: 检查手机网络状态
	 * @param context
	 *            上下文对象
	 */
	public static void CheckNetworkState(Context context) {
		boolean flag = false;
		ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); // wifi管理
		State mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); // 手机3G网络或是2G网络
		State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); // 获得手机wifi网络
		System.out.println(wifi);
		if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
			mWifiManager.setWifiEnabled(false);
			return;
		}
		if (mWifiManager.isWifiEnabled()) {
			mWifiManager.setWifiEnabled(false);
		}
		setMobileNetEnable(context);
	}

	/**
	 * 
	 * @Title: setMobileNetEnable
	 * @Description: 打开3G网络
	 * @param 设定文件
	 */
	public final static void setMobileNetEnable(Context context) {
		ConnectivityManager mConnectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		Object[] arg = null;
		try {
			boolean isMobileDataEnable = invokeMethod("getMobileDataEnabled", arg, context);
			if (!isMobileDataEnable) {
				invokeBooleanArgMethod("setMobileDataEnabled", true, context);
			}
		} catch (Exception e) {
			EvtLog.d(TAG, e.toString());
		}
	}

	/**
	 * 
	 * @Title: invokeMethod
	 * @Description: 反射判断3g是否打开
	 * @param methodName
	 *            方法名字
	 * @param arg
	 *            参数
	 * @param context
	 *            上下文对象
	 * @return boolean 返回类型 ture表示3G已经打开
	 * @throws
	 */
	private static boolean invokeMethod(String methodName, Object[] arg, Context context) throws Exception {
		ConnectivityManager mConnectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		Class<? extends ConnectivityManager> ownerClass = mConnectivityManager.getClass();
		Class<?>[] argsClass = null;
		if (arg != null) {
			argsClass = new Class[1];
			argsClass[0] = arg.getClass();
		}
		Method method = ownerClass.getMethod(methodName, argsClass);
		Boolean isOpen = (Boolean) method.invoke(mConnectivityManager, arg);
		return isOpen;
	}

	/**
	 * 
	 * @Title: invokeBooleanArgMethod
	 * @Description: 利用反射打开3g
	 * @param methodName
	 *            方法名字
	 * @param value
	 *            是否打开3G
	 * @return Object 返回类型
	 */
	private static Object invokeBooleanArgMethod(String methodName, boolean value, Context context) throws Exception {
		ConnectivityManager mConnectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		Class<? extends ConnectivityManager> ownerClass = mConnectivityManager.getClass();
		Class<?>[] argsClass = new Class[1];
		argsClass[0] = boolean.class;
		Method method = ownerClass.getMethod(methodName, argsClass);
		return method.invoke(mConnectivityManager, value);
	}

}
